package MyStack;

import java.util.Arrays;

public class MyStack<E> implements Operations{

    public int[] arr;

    public int usedSize;

    public MyStack() {
        this.arr = new int[10];
    }

    //判断栈是否满了
    private boolean isFull() {
        return this.usedSize == this.arr.length;
    }

    @Override
    public int push(int data) {
        if (isFull()) {
            this.arr = Arrays.copyOf(arr, 2 * this.arr.length);
        }

        this.arr[this.usedSize++] = data;
        return data;
    }

    @Override
    public int pop() {
        if (empty()) {
            throw new EmptyStackException("栈为空，无法出栈");
        }
        int val = this.arr[--this.usedSize];

        return val;
    }

    @Override
    public int peek() {
        return this.arr[this.usedSize - 1];
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public boolean empty() {
        return this.usedSize == 0;
    }
}
